﻿using System;
using System.Linq;
using System.Collections;
using System.Data;
using System.Web.UI.WebControls;
using ASPNETVN.PORTAL.Components.Helpers;
using ASPNETVN.PORTAL.Components.Share;
using ASPNETVN.PORTAL.Components.Share.Controls;
using ASPNETVN.PORTAL.Components.UI;

namespace ASPNETVN.PORTAL.Modules.DOCUMENT.Controls
{
    public partial class ItemWithMultipleCategories : ModulePage
    {
        protected string GetAddEditItemUrl(int itemID)
        {
            return HttpUrlBuilder.BuildAddEditUrl(this.ModuleID, itemID, "AddEditItem");
        }

        private void BuildPageLink()
        {
            if (Request.QueryString["CategoryID"] != null)
            {
                int categoryID = Convert.ToInt32(Request.QueryString["CategoryID"]);
                PageLink pagelink = (PageLink)this.ParentControl.FindControl("MainPageLink");
                Category category = Category.Load(categoryID);
                if (category != null)
                {
                    Category parentCategory = Category.Load(category.ParentID);
                    if (parentCategory != null)
                    {
                        pagelink.AddLink(parentCategory.Title, ResolveClientUrl(HttpUrlBuilder.BuildModulePageUrl(this.ModuleID, 0, ModulePages.Item, "categoryid={0}", parentCategory.ID)));

                        string url = ResolveClientUrl(HttpUrlBuilder.BuildModulePageUrl(this.ModuleID, 0, ModulePages.Item, "categoryid={0}", category.ID));
                        pagelink.AddLink(category.Title, url);
                    }
                    else
                    {
                        pagelink.AddLink(category.Title, ResolveClientUrl(HttpUrlBuilder.BuildModulePageUrl(this.ModuleID, 0, ModulePages.Item, "categoryid={0}", category.ID)));
                    }
                }
            }
        }

        private void GridDataBinding(int pageSize, int pageIndex)
        {
            DataTable dt;
            if (Request.QueryString["CategoryID"] != null)
            {
                dt = Components.Item.GetItemsByCategoryID(Int32.Parse(Request.QueryString["categoryid"]), pageSize, pageIndex).Tables[0];
            }
            else
            {
                if (Request.QueryString["OptionView"] != null)
                {
                    dt = Components.Item.GetTopItems(this.ModuleID, Request.QueryString["OptionView"], pageSize, pageIndex).Tables[0];

                    if (Request.QueryString["OptionView"] == "PublishedDate")
                    {
                        var query1 = (from r in dt.AsEnumerable()
                                      select new { Code = r.Field<string>("Code"), PublishedDate = r.Field<DateTime>("PublishedDate"), Summary = r.Field<string>("Summary") }).OrderByDescending(x => x.PublishedDate).Distinct().Take(20);
                        gvItem.DataSource = query1;
                    }
                    else
                    {
                        var query2 = (from r in dt.AsEnumerable()
                                      select new { Code = r.Field<string>("Code"), CreatedDate = r.Field<DateTime>("CreatedDate"), PublishedDate = r.Field<DateTime>("PublishedDate"), Summary = r.Field<string>("Summary") }).OrderByDescending(x => x.CreatedDate).Select(x => new { Code = x.Code, PublishedDate = x.PublishedDate, Summary = x.Summary }).Distinct().Take(20);
                        gvItem.DataSource = query2;
                    }
                }
                else
                {
                    dt = Components.Item.GetTopItems(this.ModuleID, string.Empty, pageSize, pageIndex).Tables[0];
                    var query1 = (from r in dt.AsEnumerable()
                                  select new { Code = r.Field<string>("Code"), PublishedDate = r.Field<DateTime>("PublishedDate"), Summary = r.Field<string>("Summary") }).OrderByDescending(x => x.PublishedDate).Distinct().Take(20);
                    gvItem.DataSource = query1;
                }
            }
           
            gvItem.DataBind();

        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                int categoryID = 0;
                if (Request.QueryString["CategoryID"] != null)
                {
                    categoryID = Convert.ToInt32(Request.QueryString["CategoryID"]);
                }

                Hashtable settings = this.Settings;
                int pageSize = Convert.ToInt32(settings["PageSize"].ToString());
                gvItem.PageSize = pageSize;

                this.BuildPageLink();
                this.GridDataBinding(Int32.MaxValue, 1);
            }
        }
    }
}